package dtu.app.fragment;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iqengines.demo.R;

import dtu.app.db.ProductDatabaseHelper;
import dtu.app.listadapter.TempProductArrayAdapter;
import dtu.app.listloader.TempProductListLoader;
import dtu.app.model.Product;

public class TempProductListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Product>> {

	TempProductArrayAdapter mAdapter;
	OnProductTempItemSelectedListener mListener;
	
	public interface OnProductTempItemSelectedListener {
	    public void onProductTempItemSelected(String category);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnProductTempItemSelectedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnProductTempItemSelectedListener");
		}
	}
	
	@Override public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		System.out.println("DataListFragment.onActivityCreated");

		// Initially there is no data 
		setEmptyText("No Data Here");

		// Create an empty adapter we will use to display the loaded data.
		mAdapter = new TempProductArrayAdapter(getActivity());
		setListAdapter(mAdapter);

		// Start out with a progress indicator.
		setListShown(false);

		// Prepare the loader.  Either re-connect with an existing one,
		// or start a new one.
		getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
	}
			
	@Override
	public Loader<List<Product>> onCreateLoader(int arg0, Bundle bundle) {
		Log.i("DataListFragment", "DataListFragment.onCreateLoader");
		return new TempProductListLoader(getActivity());
	}

	@Override
	public void onLoadFinished(Loader<List<Product>> arg0, List<Product> products) {
		mAdapter.setData(products);
		Log.i("DataListFragment", "DataListFragment.onLoadFinished");

		// The list should now be shown.
		if (isResumed()) {
			setListShown(true);
		} else {
			setListShownNoAnimation(true);
		}
	}

	@Override
	public void onLoaderReset(Loader<List<Product>> arg0) {
		mAdapter.setData(null);
	}
		
	public void addItemsToStock(){

		ProductDatabaseHelper db = new ProductDatabaseHelper(getActivity());
		List<Product> tempProducts = db.getAllTempProducts();

		for (Product tempProduct : tempProducts) {
			Product inStockProduct = db.getProductByName(tempProduct.getName(), false);
			if (inStockProduct != null) {
				inStockProduct.setInStockPieces(inStockProduct.getInStockPieces() + tempProduct.getTotalPieces());
				inStockProduct.setInStockAmount(inStockProduct.getInStockAmount() + tempProduct.getTotalAmount());
				db.updateProduct(inStockProduct, false);
			} else {
				inStockProduct = new Product();
				inStockProduct.setCategory(tempProduct.getCategory());
				inStockProduct.setImagePath(tempProduct.getImagePath());
				inStockProduct.setInStockAmount(tempProduct.getTotalAmount());
				inStockProduct.setInStockPieces(tempProduct.getTotalPieces());
				inStockProduct.setUnit(tempProduct.getUnit());
				inStockProduct.setPieces(tempProduct.getPieces());
				inStockProduct.setName(tempProduct.getName());
				inStockProduct.setPiecesAmount(tempProduct.getPiecesAmount());
				db.insertProduct(inStockProduct, false);
			}
		}
		db.clearTable(true);
		Toast.makeText(getActivity(), "Adding products..", Toast.LENGTH_SHORT).show();
		
		getLoaderManager().restartLoader(0, null, this);
		mAdapter.notifyDataSetChanged();
	}
	
}